home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SoundArea.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  122 lines

  1. /*
  2.  * @(#)SoundArea.java    1.9 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.applet.AudioClip;
  33. import java.net.URL;
  34. import java.net.MalformedURLException;
  35.  
  36. /**
  37.  * An audio feedback ImageArea class.
  38.  * This class extends the basic ImageArea Class to play a sound when
  39.  * the user enters the area.
  40.  *
  41.  * @author     Jim Graham
  42.  * @author     Chuck McManis
  43.  * @version     1.9, 03/18/98
  44.  */
  45. class SoundArea extends ImageMapArea {
  46.     /** The URL of the sound to be played. */
  47.     URL sound;
  48.     AudioClip soundData = null;
  49.     boolean hasPlayed; 
  50.     boolean isReady = false;
  51.     long    lastExit = 0;
  52.     final static int HYSTERESIS = 1500;
  53.  
  54.     /**
  55.      * The argument is the URL of the sound to be played.
  56.      */
  57.     public void handleArg(String arg) {
  58.     try {
  59.         sound = new URL(parent.getDocumentBase(), arg);
  60.     } catch (MalformedURLException e) {
  61.         sound = null;
  62.     }
  63.     hasPlayed = false;
  64.     }
  65.  
  66.     /**
  67.      * The applet thread calls the getMedia() method when the applet
  68.      * is started.
  69.      */
  70.     public void getMedia() {
  71.     if (sound != null && soundData == null) {
  72.         soundData = parent.getAudioClip(sound);
  73.     }
  74.     if (soundData == null) {
  75.         System.out.println("SoundArea: Unable to load data "+sound);
  76.     }
  77.     isReady = true;
  78.     }
  79.  
  80.     /**
  81.      * The enter method is called when the mouse enters the area.
  82.      * The sound is played if the mouse has been outside of the
  83.      * area for more then the delay indicated by HYSTERESIS.
  84.      */
  85.     public void enter() {
  86.     // is the sound sample loaded?
  87.     if (! isReady) {
  88.         parent.showStatus("Loading media file...");
  89.         return;
  90.     }
  91.  
  92.     /*
  93.       * So we entered the selection region, play the sound if
  94.      * we need to. Track the mouse entering and exiting the
  95.      * the selection box. If it doesn't stay out for more than
  96.      * "HYSTERESIS" millis, then don't re-play the sound.
  97.      */
  98.     long now = System.currentTimeMillis();
  99.     if (Math.abs(now - lastExit) < HYSTERESIS) {
  100.         // if within the window pretend that it was played.
  101.         hasPlayed = true;
  102.             return;
  103.     }
  104.  
  105.     // Else play the sound.
  106.     if (! hasPlayed && (soundData != null)) {
  107.         hasPlayed = true;
  108.         soundData.play();
  109.     }
  110.     }
  111.  
  112.     /**
  113.      * The exit method is called when the mouse leaves the area.
  114.      */
  115.     public void exit() {
  116.     if (hasPlayed) {
  117.         hasPlayed = false;
  118.         lastExit = System.currentTimeMillis(); // note the time of exit
  119.     }
  120.     }
  121. }
  122.